<template>
  <el-dialog :visible.sync="isShow" v-if="isShow" :append-to-body="true" title="KOL申请" width="52%" :before-close="handleClose" top="150px" :close-on-click-modal="false">
    <el-form ref="ruleForm" :model="formParams" :rules="ruleForm" label-width="0">
      <div class="flex item-box" v-for="(item,index) in itemData" :key="index">
        <div class="title width25">
          {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
        </div>
        <div class="content width80">
          <el-form-item class="item-clear" v-show="item.key=='user_id'" label="" prop="user_id">
            <el-input-number :min="0" :max="999999999999" :precision="0" v-model="formParams.user_id" controls-position="right" clearable placeholder="请输入用户ID"  />
          </el-form-item>
          <el-form-item class="item-clear" v-show="item.key=='amount'" label="" prop="amount">
            <el-input-number :min="1" :max="999999999" :precision="0" v-model="formParams.amount" controls-position="right" clearable placeholder="请输入审核金额"  />
          </el-form-item>
          <el-form-item class="item-clear" v-show="item.key=='reason'" label="" prop="reason" style="height:auto;padding-bottom:5px">
            <el-input type="textarea" rows="4" v-model="formParams.reason" placeholder="请输入审核理由"/>
          </el-form-item>
          <el-form-item class="item-clear" v-show="item.key=='images'" label="" prop="images" style="height:auto;padding-bottom:5px">
            <imgUpLoad :url="formParams.images" @success='(url)=>{imgUploadChang(url)}' />
          </el-form-item>
        </div>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>
 
<script>
import {
  KolApply
} from '@/api/kol'
import { 
  getDetails
} from "@/api/agent";
import {
  userWhite,
} from '@/api/player'
import imgUpLoad from '@/components/imgUpLoadSingle'
export default {
  components: {
    imgUpLoad
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
    type: {
      type: Number,
      default: 1
    },
  },
  data () {
    return {
      isShow: false,
      itemData:[
        { label: '用户ID', key: 'user_id',pass:true },
        { label: '审核金额', key: 'amount',pass:true },
        { label: '审核理由', key: 'reason' ,pass:true },
        { label: '审核凭证', key: 'images' },
      ],
      formParams:{
        user_id:0,
        amount:0,
        reason:'',
        images:'',
        types:this.type
      },
      ruleForm: {
        user_id: [{ required: true, trigger: 'blur', message: '请输入审核用户ID', }],
        amount: [{ required: true, trigger: 'blur', message: '请输入审核金额', }],
        reason: [{ required: true, trigger: 'blur', message: '请输入审核理由', }],
      }
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
        that.formParams={
          user_id:0,
          amount:0,
          reason:'',
          images:'',
          types:that.type
        }
    },
  },
  mounted () {},
  methods: {
    // 确认
    handleConfirm (formName) {
      let that = this, params = JSON.parse(JSON.stringify(that.formParams));
      params.amount=that.$public.mathNumberMultiply(params.amount,10000)
      that.$refs[formName].validate((valid) => {
        if (valid) {
          KolApply(params).then(function (res) {
            that.$message.success(res.msg)
            that.$emit('onHideDetails', true)
          }).catch((error) => {
            if(error.msg=='not is kol user'){
              that.getDetailsList(params.user_id)
            }else{
              that.$message.error(error.msg)
            }
          })
        } else {
          return false;
        }
      });
    },
    // 用户信息
    getDetailsList (id) {
      let that = this;
      that.listLoading = true;
      getDetails({user_id:id}).then(function (res) {
        if (res.code==1) {
          if(res.data.userInfo && res.data.userInfo.bank_info.mobile){
            that.formParams.account_type=res.data.userInfo.account_type
            that.setAccount()
          }else{
            that.$message.error('该用户不是kol')
          }
        }
      })
      .catch((error) => {
        that.$message.error(error.msg);
      });
    },
    // 设置账号类型
    setAccount(){
      let that = this,row=that.formParams;
      that.$confirm('是否直接设置改用户为KOL模式?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userWhite({
          account_type: row.account_type,
          is_kol: 1,
          user_id: row.id,
          reason: ''
        }).then(res => {
          if (res.code == 1) {
            that.$message({
              type: 'success',
              message: res.msg
            })
            that.onHideDetails()
            that.getAccountList()
          }
        }).catch((error) => {
          that.$message.error(error.msg)
        })
      }).catch(() => {});
    },
    imgUploadChang (url) {
      this.formParams.images = url
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
</style>